import {fileURLToPath, URL} from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
const external = ['vue', '@ant-design', 'ant-design-vue', 'lodash', '@ant-design/icons-vue', '@/api/sys/roleApi', '@/api/sys/userApi', '@/api/sys/orgApi', '@/api/components/Selector/userSelectorPlusApi', '@/api/auth/sso2Api', 'vue-router', 'lodash-es',
	'@/layout/index',
	'@/views/auth/login/login',
	'@/views/auth/findPwd/index',
	'@/views/auth/login/callback',
	'@/views/auth/login/ssoCallback',
	'@/views/auth/login/codeCallback',
	'@/views/other/index',
	'vue-i18n',
	'ant-design-vue/es/locale/zh_CN',
	'ant-design-vue/es/locale/en_GB',
	'dayjs/locale/zh-cn',
	'@ant-design/colors',
	'axios',
	'sm-crypto',
	'snowy-form-design/dist/style.css',
	'snowy-form-design',
	'highlight.js/styles/atom-one-dark.css',
	'highlight.js/lib/common',
	'@highlightjs/vue-plugin',
	'vuedraggable-es',
	'ant-design-vue/es/table/Table.js',
	'ant-design-vue/es/tooltip',
	'@/api/file/personalDirectoryApi',
	'@/api/sys/positionApi',
	'@/api/components/Selector/orgSelectorPlusApi',
	'@/api/components/Selector/posSelectorPlusApi',
	'@/api/components/Selector/roleSelectorPlusApi',
	'@/api/sys/userCenterApi',
	'@/api/dev/fileApi',
	'@/api/dev/emailApi',
	'@tinymce/tinymce-vue',
	'tinymce/tinymce',
	'@/api/dev/dictApi',
	'@/api/dev/configApi',
	'@/api/dev/logApi',
	'@antv/g2plot',
	'@/api/gen/genBasicApi',
	'@/api/gen/genConfigApi',
	'@/api/sys/resource/buttonApi',
	'@/api/sys/resource/fieldApi',
	'@/api/sys/resource/menuApi',
	'@/api/sys/resource/moduleApi',
	'@/api/sys/resource/spaApi',
	'vue3-colorpicker',
	'snowflake-id',
	'@/api/sys/permissionApi',
	'@/router',
	'@/store',
	'dayjs',
	'elead-ui-tool-v3',
	'pinyin',
	'NProgress',
	"screenfull",
	"layout/index.vue"
]
const outGlobals = {
	globals: {
		vue: 'Vue',
		'axios': 'axios'
	}
}
const plugins = [
	vue({
		script: {
			refTransform: true
		}
	}),
	vueJsx(),
	AutoImport({
		dts: 'src/auto-imports.d.ts',
		imports: ['vue']
	}),
	Components({
		dirs: ['src/components'],
		dts: false,
		resolvers: []
	})
]
const css = {
	preprocessorOptions: {
		less: {
			modifyVars: {},
			javascriptEnabled: true
			//plugins: [new Less2CssVariablePlugin()]
		}
	},
	modules: {
		scopeBehaviour: {
			vue: 'Vue'
		}
	}
}
const extensions = ['.js', '.ts', '.json', '.vue']
const alias = {
	'@': fileURLToPath(new URL('./src', import.meta.url)),
	'@/api': fileURLToPath(new URL('./src/api', import.meta.url)),
	'@/components': fileURLToPath(new URL('./src/components', import.meta.url)),
	'@/views': fileURLToPath(new URL('./src/views', import.meta.url)),
	'@/utils': fileURLToPath(new URL('./src/utils', import.meta.url)),
	'@/routerUtil': fileURLToPath(new URL('./src/routerUtil', import.meta.url)),
	'@/locales': fileURLToPath(new URL('./src/locales', import.meta.url)),
	'@/comDefault': fileURLToPath(new URL('./src/comDefault', import.meta.url)),
	'@/pagesDefault': fileURLToPath(new URL('./src/pagesDefault', import.meta.url))
}
// 发布库的设置
const lib = defineConfig({
	plugins,
	resolve: {
		extensions,
		alias
	},
	css,
	optimizeDeps: {exclude: external},
	build: {
		sourcemap: true,
		target: 'esnext', // 输出的目标环境
		minify: 'terser', // 选择压缩工具（Vite默认使用terser）
		lib: {
			entry: 'src/index.ts', // 库的入口文件
			name: 'ebv3', // UMD格式下暴露的全局变量名
			fileName: (format) => `index.js`,
			formats: ['es']
		},
		rollupOptions: {
			// 确保外部化处理那些你不想打包进库的依赖
			external: external,
			output: {
				// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
				...outGlobals,
				exports: 'named',
			}
		}
	}
})
const pro = defineConfig({
	plugins,
	resolve: {
		extensions,
		alias
	},
	css,
	optimizeDeps: {exclude: external},
	build: {
		sourcemap: true,
		target: 'esnext', // 输出的目标环境
		minify: 'terser', // 选择压缩工具（Vite默认使用terser）
		lib: {
			entry: 'src/index.ts', // 库的入口文件
			name: 'ebv3', // UMD格式下暴露的全局变量名
			fileName: (format) => `index.js`,
			formats: ['es']
		},
		rollupOptions: {
			// 这里可以添加Rollup的插件和其他配置项
			external,
			input: ['src/index.ts'],
			output: [
				{
					// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
					...outGlobals
				},
				{
					//打包格式
					format: 'es',
					//打包后文件名
					entryFileNames: '[name].mjs',
					//让打包目录和我们目录对应
					preserveModules: true,
					exports: 'named',
					//配置打包根目录
					dir: './dist/es',
					...outGlobals,
					//chunkFileNames:'css/[name].css'
				},
				{
					//打包格式
					format: 'cjs',
					//打包后文件名
					entryFileNames: '[name].js',
					//让打包目录和我们目录对应
					preserveModules: true,
					exports: 'named',
					//配置打包根目录
					dir: './dist/lib',
					...outGlobals
					//	chunkFileNames:'css/[name].css'
				}
			]
		}
	}
})
export default ({mode}) => {
	const env = loadEnv(mode, process.cwd()).VITE_ENV
	if (env === 'liweihuaDEV') {
		// 打包库文件
		return lib
	} else {
		// 开发模式、生产模式
		return pro
	}
}
